/**
 * Created by fkjslee on 2018/2/18.
 */

var logAccount = "no account";
var serverUrl = "http://118.24.119.103:8081";
// var serverUrl = "http://127.0.0.1:8081";
var HTTP = "http://"

window.onload = autoRun();

$("#download-files").datagrid({
    onLoadSuccess: fillFileState(),
    idField: "id"
});

$("#station-state").datagrid({
    onLoadSuccess: fillStationState(),
    idField: "id"
});

//run when the html was loaded
function autoRun() {
    self.setInterval(loadAccountMsg, 1000);
    var name = "account";
    var strCookie = document.cookie;
    var arrCookie = strCookie.split("; ");
    for (var i = 0; i < arrCookie.length; i++) {
        var arr = arrCookie[i].split("=");
        if (arr[0] === name) logAccount = arr[1];
    }
}

function loadAccountMsg() {
    $("#accountName").textbox().textbox('setValue', logAccount);
    if (logAccount !== "no account") {
        var request = new XMLHttpRequest();
        request.open("POST", HTTP + window.location.host + "/getAccountRunningTask?userName=" + logAccount);
        request.send();
        request.onreadystatechange = function () {
            if (request.readyState === 4 && request.status === 200) {
                var json = JSON.parse(request.responseText);
                if (json.code === "0") {
                    $("#accountRunningTask").textbox().textbox('setValue', json.number);
                }
            }
        }
        var request2 = new XMLHttpRequest();
        request2.open("POST", HTTP + window.location.host + "/getAccountRemainTask?userName=" + logAccount);
        request2.send();
        request2.onreadystatechange = function () {
            if (request2.readyState === 4 && request2.status === 200) {
                var json = JSON.parse(request2.responseText);
                if (json.code === "0") {
                    $("#accountRemainTask").textbox().textbox('setValue', json.number);
                }
            }
        }
    }
}

function init() {
    $("#tableParam").datagrid("appendRow", {
        thAgentName: "MAXSUMADSSVPLSALS",
        thFieldName: "MAX_CYCLE",
        thFieldType: "hyper",
        thFieldValue: "1600.00"
    });

    $("#tableParam").datagrid("appendRow", {
        thAgentName: "MAXSUMADSSVPLSALS",
        thFieldName: "LOCAL_SEARCH_ALGORITHM",
        thFieldType: "discrete",
        thFieldValue: "DSA,MGM,MGM2"
    });

    $("#tableParam").datagrid("appendRow", {
        thAgentName: "MAXSUMPVPALS",
        thFieldName: "scale",
        thFieldType: "discrete",
        thFieldValue: "linear,positive_quadratic,negative_quadratic,exponential"
    });

    $("#tableParam").datagrid("appendRow", {
        thAgentName: "MAXSUMADSSVPLSALS",
        thFieldName: "TIMING",
        thFieldType: "continuous",
        thFieldValue: "L:1.00,  U:2.00,  S:3.00"
    });
}

function submitParam() {
    var jsonStr = JSON.stringify($("#tableParam").datagrid("getRows"));
    var formData = new FormData($("#importFileForm")[0]);
    formData.append("paramGroup", jsonStr);
    formData.append("userName", logAccount);
    $.ajax({
        url: HTTP + window.location.host + '/assignTask',
        type: 'POST',
        data: formData,
        async: false,
        cache: false,
        contentType: false,
        processData: false,
        success: function (data) {
            if (JSON.parse(data).code === "0") {
                $.messager.show({
                    title: "提交结果",
                    msg: "提交成功，等候处理",
                    type: "show"
                })
            } else {
                $.messager.show({
                    title: "提交结果",
                    msg: "出现问题 返回结果:" + data,
                    type: "show"
                })
            }
        },
        error: function (returndata) {
        }
    });
}

function fillStationState() {
    var stationState = $("#station-state");
    if (stationState === null) return;
    var rows = stationState.datagrid('getRows');
    if (rows) for (var i = rows.length - 1; i >= 0; --i) {
        stationState.datagrid('deleteRow', i);
    }
    var request = new XMLHttpRequest();
    request.open("POST", HTTP + window.location.host + "/getStationState");
    request.send();
    request.onreadystatechange = function () {
        if (request.readyState === 4 && request.status === 200) {
            if (request.responseText === "") {
                return;
            }
            var json = JSON.parse(request.responseText);
            for (var i = 0; i < json.length; ++i) {
                stationState.datagrid("appendRow", {
                    id: i,
                    thStationName: json[i].stationName,
                    thStationIP: json[i].stationIP,
                    thStationTaskNum: json[i].stationTaskNum,
                    thStationState: (json[i].stationTaskNum === "0" ? "空闲" : "繁忙")
                })
            }
        }
    }
}

function fillFileState() {
    var downloadFiles = $("#download-files");
    if (downloadFiles === null) return;
    var rows = downloadFiles.datagrid('getRows');
    if (rows) for (var i = rows.length - 1; i >= 0; i--) {
        downloadFiles.datagrid('deleteRow', i);
    } else {
        return;
    }
    var request = new XMLHttpRequest();
    request.open("POST", HTTP + window.location.host + "/getFinishedFileName?userName=" + logAccount);
    request.send();
    request.onreadystatechange = function () {
        if (request.readyState === 4 && request.status === 200) {
            var json = JSON.parse(request.responseText);
            for (var i = 0; i < json.length; ++i) {
                downloadFiles.datagrid("appendRow", {
                    id: i,
                    thFileName: json[i].name,
                    thTime: json[i].time
                });
            }
        }
    }
}